/**
 * Created by lx on 2018/4/3.
 */
CommonUtils.regNamespace("com.im.fixedMoneyManage.baos");

$(function() {
    com.im.fixedMoneyManage.baos.invoke.init();
});

com.im.fixedMoneyManage.baos.invoke = function() {
    var client = new com.im.common.ajax();
    var tmpfiles;//文件上传下载

    /**
     * 页面初始化
     * @private
     */
    function _init() {
        //初始化时间、复选框、下拉框等事件和样式
        _initCommpentAction();
        _initFormValidate();
        //查询
        $("#queryMoneys").on("click", function() {
            _queryMoneys(1);
        });
        //条件重置
        $("#resetCond").on("click", _resetCond);
        //资金计划新增
        $("#addMoney").on("click", function() {
            _showEditMoney();
        });
        //修改资金计划
        $("#editMoney").on("click", _editMoney);
        //查询-所属规划-建设单位-实施项目
        $("#planTypeSel").project2unit2itemCommpent({
            elmIdPlanTypeSel : "planTypeSel",
            elmIdUnitSel : "orgUnit",//组织单位下拉元素
            elmIdItemSel : "impleProjName",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
                _queryMoneys(1);
            }
        });
        //新增编辑-所属规划-建设单位to实施项目
        $("#planTypeSel2").project2unit2itemCommpent({
            elmIdPlanTypeSel : "planTypeSel2",
            elmIdUnitSel : "orgUnit2",//组织单位下拉元素
            elmIdItemSel : "impleProjName2",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
                //_callBackOrg(objvalue, objname);
                //callBackType : 1 所属规划值变更 2单位值变更  3项目名称变更
                //ret: orgId,orgName,impleProjId,impleProjName,planProjClass,planProjClassName
                if(callBackType == 3) {
                    var req = {};
                    req.impleProjId = ret.impleProjId;
                    req.declareType = 20;
                    var resp = client.callAsJson("fundPlanService/queryFundPlanListForGreen", req);
                    if (resp.success) {
                        var fund1 = _getFundDto(resp.result.fundDetailVo,'planProjMoney');
                        var fund2 = _getFundDto(resp.result.fundDetailVo,'implProjMoney');
                        var fund3 = _getFundDto(resp.result.fundDetailVo,'investPlanMoney');

                        $("#buFund1").val(fund1.fundBu);
                        $("#shengFund1").val(fund1.fundSheng);
                        $("#gaoFund1").val(fund1.fundGao);
                        $("#qiFund1").val(fund1.fundQi);
                        $("#shiFund1").val(fund1.fundShi);
                        $("#otherFund1").val(fund1.fundOther);
                        $("#total1").val(fund1.total);

                        $("#buFund2").val(fund2.fundBu);
                        $("#shengFund2").val(fund2.fundSheng);
                        $("#gaoFund2").val(fund2.fundGao);
                        $("#qiFund2").val(fund2.fundQi);
                        $("#shiFund2").val(fund2.fundShi);
                        $("#otherFund2").val(fund2.fundOther);
                        $("#total2").val(fund2.total);

                        $("#buFund3").val(fund3.fundBu);
                        $("#shengFund3").val(fund3.fundSheng);
                        $("#gaoFund3").val(fund3.fundGao);
                        $("#qiFund3").val(fund3.fundQi);
                        $("#shiFund3").val(fund3.fundShi);
                        $("#otherFund3").val(fund3.fundOther);
                        $("#total3").val(fund3.total);

                         /*
                        //本次投资计划里面的金额，默认剩余的额度
                        $("#buFund4").val(fund1.fundBu-fund2.fundBu);
                        $("#shengFund4").val(fund1.fundSheng-fund2.fundSheng);
                        $("#gaoFund4").val(fund1.fundGao-fund2.fundGao);
                        $("#qiFund4").val(fund1.fundQi-fund2.fundQi);
                        $("#shiFund4").val(fund1.fundShi-fund2.fundShi);
                        $("#otherFund4").val(fund1.fundOther-fund2.fundOther);
                        if(fund1.fundBu-fund2.fundBu < 0){
                            $("#buFund4").val(0);
                        }
                        if(fund1.fundSheng-fund2.fundSheng < 0){
                            $("#shengFund4").val(0);
                        }
                        if(fund1.fundGao-fund2.fundGao < 0){
                            $("#gaoFund4").val(0);
                        }
                        if(fund1.fundQi-fund2.fundQi < 0){
                            $("#qiFund4").val(0);
                        }
                        if(fund1.fundShi-fund2.fundShi < 0){
                            $("#shiFund4").val(0);
                        }
                        if(fund1.fundOther-fund2.fundOther < 0){
                            $("#otherFund4").val(0);
                        }
                        if(fund1.total-fund2.total < 0){
                            $("#total4").val(0);
                        }
                        */

                        $("#buFund4").val(0);
                        $("#shengFund4").val(0);
                        $("#gaoFund4").val(0);
                        $("#qiFund4").val(0);
                        $("#shiFund4").val(0);
                        $("#otherFund4").val(0);
                        $("#total4").val(0);

                        _initTotal();
                    }
                }
            }
        });
        //初始化第一页
        _queryMoneys(1);

        //批量审定
        $("#batchAudit").click(function () {
            _batchAudit();
        });
        //导出
        $('#export').click(function (){
            _export();
        });
    }

    function _initCommpentAction() {
        $('#yearDt').datepicker({
            startView: 'decade',
            minView: 'decade',
            format: 'yyyy',
            maxViewMode: 2,
            minViewMode:2,
            autoclose: true
        });

        /*
        $('#endDt').datepicker({
            startView: 'decade',
            minView: 'decade',
            format: 'yyyy',
            maxViewMode: 2,
            minViewMode:2,
            autoclose: true
        });
        */

        //复选框
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass   : 'iradio_minimal-blue'
        });
        //下拉框
        $('.select2').select2();



        var req = {};
        var result = {};
        result = client.callAsJson("fixAssetService/queryFixAssetYearDtList", req);
        if (result.success) {
            var year = $("#year");
            $.each(result.result.statusCds, function (i, item) {
                if(i == 0) {
                    year.append("<option selected='selected' value=" + item + ">" +item + "</option>");
                } else {
                    year.append("<option value=" + item + ">" +item + "</option>");
                }
            });
        }

        $('#year').change(function(){
            _queryMoneys(1);
        });

        /*
        req = {};
        req.tabName="T_FIX_ASSET";
        req.colName="STATUS_CD";
        result = client.callAsJson("commonService/queryTabColParam", req);
        if (result.success) {
            var states = $("#states");
            $.each(result.result, function (i, item) {
                if(i == 0) {
                    states.append("<option selected='selected' value=" + item.colVal + ">" +item.colValName + "</option>");
                } else {
                    states.append("<option value=" + item.colVal + ">" +item.colValName + "</option>");
                }
            });
        }
        */

        //失去聚焦
        $("#buFund4").keyup(function () {
            _initTotal();
        });
        $("#shengFund4").keyup(function () {
            _initTotal();
        });
        $("#gaoFund4").keyup(function () {
            _initTotal();
        });
        $("#qiFund4").keyup(function () {
            _initTotal();
        });
        $("#shiFund4").keyup(function () {
            _initTotal();
        });
        $("#otherFund4").keyup(function () {
            _initTotal();
        });
    }

    function _initTotal() {
        var b = $("#buFund4").val();
        var s = $("#shengFund4").val();
        var g = $("#gaoFund4").val();
        var q = $("#qiFund4").val();
        var sh = $("#shiFund4").val();
        var o= $("#otherFund4").val();

        var tmp = 0;
        if(b != ''){
            tmp = tmp + parseFloat(b);
        }
        if(s != ''){
            tmp = tmp + parseFloat(s);
        }
        if(g != ''){
            tmp = tmp + parseFloat(g);
        }
        if(q != ''){
            tmp = tmp + parseFloat(q);
        }
        if(sh != ''){
            tmp = tmp + parseFloat(sh);
        }
        if(o != ''){
            tmp = tmp + parseFloat(o);
        }
        $("#total4").val(tmp.toFixed(2));
    }

    /**
     * 表单验证
     * @param $form
     * @returns {boolean}
     * @private
     */
    function _formValidate($form) {
        $form.data("bootstrapValidator").validate();
        return $form.data("bootstrapValidator").isValid();
    }

    /**
     * 表单验证初始化
     * @private
     */
    function _initFormValidate(flag) {
        $("#addEditMoneyForm").bootstrapValidator({
            live: 'enabled',
            excluded: [':hidden'],
            feedbackIcons: {valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-loadData'},
            fields: {
                impleProjName2: {validators: {notEmpty: {message: "请选择实施项目"}}},
                /*orgUnit2: {validators: {notEmpty: {message: "请选择建设单位"}}},*/
                yearDt: {trigger:"change",validators: {notEmpty: {message: "请选择年份"}}},
                /*
                endDt: {trigger:"change",validators: {notEmpty: {message: "请选择截止日期"},
                    callback: {message: "截止日期不能小于年份", callback:function(value, validator,$field){
                        if($("#yearDt").val() == null || $("#yearDt").val() == "" ||  $("#endDt").val() == null||  $("#endDt").val() == ""){
                            return true;
                        }
                        return new Date($("#yearDt").val()) <= new Date($("#endDt").val());
                    }}
                }},
                */
                buFund4: {validators: {notEmpty: {message: "请填写部补助金额"}, numeric: {message: '部补助只能输入数字'}}},
                shengFund4: {validators: {notEmpty: {message: "请填写省财政金额"}, numeric: {message: '省财政只能输入数字'}}},
                gaoFund4: {validators: {notEmpty: {message: "请填写政府融资金额"}, numeric: {message: '政府融资只能输入数字'}}},
                qiFund4: {validators: {notEmpty: {message: "请填写部企业自筹金额"}, numeric: {message: '企业自筹只能输入数字'}}},
                shiFund4: {validators: {notEmpty: {message: "请填写市州配套金额"}, numeric: {message: '市州配套只能输入数字'}}}
                //, description: {validators: {notEmpty: {message: "请填写备注"}}}
            }
        });

        if(flag == undefined) {
            $('#addEditModal').on('hidden.bs.modal', function () {
                $('#addEditMoneyForm').data('bootstrapValidator').destroy();
                $('#addEditMoneyForm').data('bootstrapValidator', null);
                _initFormValidate("again");
            });
        }
    }

    /**
     * 条件重置
     * @private
     */
    function _resetCond() {
        $('#orgUnit,#impleProjName').val('');
        $('#orgUnit').val("").trigger("change");
        $('#impleProjName').val("").trigger("change");
        $("#year option:first").prop("selected", 'selected').trigger("change");
        _queryMoneys(1);
    }

    /**
     * 固定资产列表查询
     * @param page
     * @param needTurn
     * @private
     */
    function _queryMoneys(index) {
        var vYear = $("#year").val();
        if(null==vYear){
            var date = new Date(date);
            vYear = date.getFullYear();
        }

            com.im.fixedMoneyManage.baos.moneyColumns = [
                [{field:'全选',title:"全选",width:30,checkbox:true,rowspan:2,fitColumns:true},
                    {field:'vaSeq',title:"序号",rowspan:2,fitColumns:true,formatter: function(value,row,index){
                        if(row.planProjMoneyTotal != undefined) {
                        return "";
                    }
                    return index + 1;}},
                    /*{field:'declareSetName',title:"资金计划申报批次",rowspan:2,width:100,fitColumns:true},*/
                    {field:'impleProjName',title:"项目名称",rowspan:2,fitColumns:true},
                    {field:'orgName',title:"建设单位",rowspan:2,fitColumns:true},
                    {field:'yearDt',title:"年份",rowspan:2,fitColumns:true,align:'center'},
                    {field:'GSTZ',colspan:7,title:'概算总投资（单位：万元）',align:'center'},
                    {field:'XDDJH',colspan:7,title:'累计至'+(vYear - 1)+'年下达的计划（单位：万元）',align:'center'},
                    {field:'TZDZJ',colspan:7,title:'累计至'+(vYear - 1)+'年完成的投资（单位：万元）',align:'center'},
                    {field:'TZJH',colspan:7,title:'本次完成投资',align:'center'},
                    {field:'statusName',title:"状态",rowspan:2,fitColumns:true},
                    {field:'vaOperation',title:"操作",rowspan:2,fitColumns:true,
                        formatter: function(value,row,index){
                        if(row.planProjMoneyTotal != undefined) {
                            return "";
                        }
                            return  "<a href='javascript:;' onclick='com.im.fixedMoneyManage.baos.invoke.showEditMoney("+ index +");' title='修改'><i class='fa fa-edit'></i></a> "+
                                "<a href='javascript:;' onclick='com.im.fixedMoneyManage.baos.invoke.deleteMoney("+ index +");' title='删除'><i class='fa fa-trash'></i></a>";
                        }
                    }],
                [
                    {field:'planProjMoneyTotal',title:"合计",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'total','planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundBu',title:"部补助",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundBu','planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundSheng',title:"省财政",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundSheng','planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundGao',title:"政府融资",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundGao','planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundShi',title:"市州配套",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row, 'fundShi', 'planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundQi',title:"企业自筹",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundQi','planProjMoney');
                        }
                    }
                    },
                    {field:'planProjMoneyFundOther',title:"其他",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundOther','planProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyTotal',title:"合计",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'total','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundBu',title:"部补助",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundBu','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundSheng',title:"省财政",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundSheng','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundGao',title:"政府融资",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundGao','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundShi',title:"市州配套",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundShi','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundQi',title:"企业自筹",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundQi','implProjMoney');
                        }
                    }
                    },
                    {field:'implProjMoneyFundOther',title:"其他",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundOther','implProjMoney');
                        }
                    }
                    },
                    {field:'investPlanTotal',title:"合计",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'total','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundBu',title:"部补助",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundBu','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundSheng',title:"省财政",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundSheng','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundGao',title:"政府融资",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundGao','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundShi',title:"市州配套",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundShi','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundQi',title:"企业自筹",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundQi','investPlanMoney');
                        }
                    }
                    },
                    {field:'investPlanFundOther',title:"其他",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundOther','investPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyTotal',title:"合计",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'total','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundBu',title:"部补助",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundBu','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundSheng',title:"省财政",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundSheng','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundGao',title:"政府融资",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundGao','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundShi',title:"市州配套",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundShi','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundQi',title:"企业自筹",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundQi','fundPlanMoney');
                        }
                    }
                    },
                    {field:'fundPlanMoneyFundOther',title:"其他",fitColumns:true
                        ,formatter: function(value,row,index){
                        if(value != undefined) {
                            return value;
                        }else {
                            return _getColVal(row,'fundOther','fundPlanMoney');
                        }
                    }
                    }
                ]
            ];
        var req = {};
        req.pageNumber = index;
        req.pageSize = 20;
        req.year = vYear;
        req.planType = $("#planTypeSel option:selected").attr("value");
        req.developOrg = $("#orgUnit option:selected").attr("value");
        req.impleProjId = $("#impleProjName option:selected").attr("value");
        var resp = client.callAsJson("fixAssetService/queryFixAssetListForUI", req);
        if (!resp.success) {
            toastr.error(resp.resultMsg);
            return;
        }

        //汇总
        resp.result.footer = [];
        resp.result.footer[0] = {};
        resp.result.footer[0].yearDt = "合计";
        resp.result.footer[0].planProjMoneyTotal = 0;
        resp.result.footer[0].planProjMoneyFundBu = 0;
        resp.result.footer[0].planProjMoneyFundSheng = 0;
        resp.result.footer[0].planProjMoneyFundGao = 0;
        resp.result.footer[0].planProjMoneyFundShi = 0;
        resp.result.footer[0].planProjMoneyFundQi = 0;
        resp.result.footer[0].planProjMoneyFundOther = 0;
        resp.result.footer[0].implProjMoneyTotal = 0;
        resp.result.footer[0].implProjMoneyFundBu = 0;
        resp.result.footer[0].implProjMoneyFundSheng = 0;
        resp.result.footer[0].implProjMoneyFundGao = 0;
        resp.result.footer[0].implProjMoneyFundShi = 0;
        resp.result.footer[0].implProjMoneyFundQi = 0;
        resp.result.footer[0].implProjMoneyFundOther = 0;
        resp.result.footer[0].investPlanTotal = 0;
        resp.result.footer[0].investPlanFundBu = 0;
        resp.result.footer[0].investPlanFundSheng = 0;
        resp.result.footer[0].investPlanFundGao = 0;
        resp.result.footer[0].investPlanFundShi = 0;
        resp.result.footer[0].investPlanFundQi = 0;
        resp.result.footer[0].investPlanFundOther = 0;
        resp.result.footer[0].fundPlanMoneyTotal = 0;
        resp.result.footer[0].fundPlanMoneyFundBu = 0;
        resp.result.footer[0].fundPlanMoneyFundSheng = 0;
        resp.result.footer[0].fundPlanMoneyFundGao = 0;
        resp.result.footer[0].fundPlanMoneyFundShi = 0;
        resp.result.footer[0].fundPlanMoneyFundQi = 0;
        resp.result.footer[0].fundPlanMoneyFundOther = 0;
        for(var i = 0; i < resp.result.rows.length; i++) {
            for(var j = 0; j < resp.result.rows[i].fundDetailVo.length; j++) {
                if(resp.result.rows[i].fundDetailVo[j].type == "planProjMoney") {
                    resp.result.footer[0].planProjMoneyTotal += resp.result.footer[0].planProjMoneyTotal + Number(resp.result.rows[i].fundDetailVo[j].total);
                    resp.result.footer[0].planProjMoneyFundBu += Number(resp.result.rows[i].fundDetailVo[j].fundBu);
                    resp.result.footer[0].planProjMoneyFundSheng += Number(resp.result.rows[i].fundDetailVo[j].fundSheng);
                    resp.result.footer[0].planProjMoneyFundGao += Number(resp.result.rows[i].fundDetailVo[j].fundGao);
                    resp.result.footer[0].planProjMoneyFundShi += Number(resp.result.rows[i].fundDetailVo[j].fundShi);
                    resp.result.footer[0].planProjMoneyFundQi += Number(resp.result.rows[i].fundDetailVo[j].fundQi);
                    resp.result.footer[0].planProjMoneyFundOther += Number(resp.result.rows[i].fundDetailVo[j].fundOther);
                }else if(resp.result.rows[i].fundDetailVo[j].type == "implProjMoney") {
                    resp.result.footer[0].implProjMoneyTotal += Number(resp.result.rows[i].fundDetailVo[j].total);
                    resp.result.footer[0].implProjMoneyFundBu += Number(resp.result.rows[i].fundDetailVo[j].fundBu);
                    resp.result.footer[0].implProjMoneyFundSheng += Number(resp.result.rows[i].fundDetailVo[j].fundSheng);
                    resp.result.footer[0].implProjMoneyFundGao += Number(resp.result.rows[i].fundDetailVo[j].fundGao);
                    resp.result.footer[0].implProjMoneyFundShi += Number(resp.result.rows[i].fundDetailVo[j].fundShi);
                    resp.result.footer[0].implProjMoneyFundQi += Number(resp.result.rows[i].fundDetailVo[j].fundQi);
                    resp.result.footer[0].implProjMoneyFundOther += Number(resp.result.rows[i].fundDetailVo[j].fundOther);
                }else if(resp.result.rows[i].fundDetailVo[j].type == "investPlanMoney") {
                    resp.result.footer[0].investPlanTotal += Number(resp.result.rows[i].fundDetailVo[j].total);
                    resp.result.footer[0].investPlanFundBu += Number(resp.result.rows[i].fundDetailVo[j].fundBu);
                    resp.result.footer[0].investPlanFundSheng += Number(resp.result.rows[i].fundDetailVo[j].fundSheng);
                    resp.result.footer[0].investPlanFundGao += Number(resp.result.rows[i].fundDetailVo[j].fundGao);
                    resp.result.footer[0].investPlanFundShi += Number(resp.result.rows[i].fundDetailVo[j].fundShi);
                    resp.result.footer[0].investPlanFundQi += Number(resp.result.rows[i].fundDetailVo[j].fundQi);
                    resp.result.footer[0].investPlanFundOther += Number(resp.result.rows[i].fundDetailVo[j].fundOther);
                }else if(resp.result.rows[i].fundDetailVo[j].type == "fundPlanMoney") {
                    resp.result.footer[0].fundPlanMoneyTotal += Number(resp.result.rows[i].fundDetailVo[j].total);
                    resp.result.footer[0].fundPlanMoneyFundBu += Number(resp.result.rows[i].fundDetailVo[j].fundBu);
                    resp.result.footer[0].fundPlanMoneyFundSheng += Number(resp.result.rows[i].fundDetailVo[j].fundSheng);
                    resp.result.footer[0].fundPlanMoneyFundGao += Number(resp.result.rows[i].fundDetailVo[j].fundGao);
                    resp.result.footer[0].fundPlanMoneyFundShi += Number(resp.result.rows[i].fundDetailVo[j].fundShi);
                    resp.result.footer[0].fundPlanMoneyFundQi += Number(resp.result.rows[i].fundDetailVo[j].fundQi);
                    resp.result.footer[0].fundPlanMoneyFundOther += Number(resp.result.rows[i].fundDetailVo[j].fundOther);
                }
            }
        }

        resp.result.footer[0].planProjMoneyTotal = _toDecimal2(resp.result.footer[0].planProjMoneyTotal);
        resp.result.footer[0].planProjMoneyFundBu = _toDecimal2(resp.result.footer[0].planProjMoneyFundBu);
        resp.result.footer[0].planProjMoneyFundSheng = _toDecimal2(resp.result.footer[0].planProjMoneyFundSheng);
        resp.result.footer[0].planProjMoneyFundGao = _toDecimal2(resp.result.footer[0].planProjMoneyFundGao);
        resp.result.footer[0].planProjMoneyFundShi = _toDecimal2(resp.result.footer[0].planProjMoneyFundShi);
        resp.result.footer[0].planProjMoneyFundQi = _toDecimal2(resp.result.footer[0].planProjMoneyFundQi);
        resp.result.footer[0].planProjMoneyFundOther = _toDecimal2(resp.result.footer[0].planProjMoneyFundOther);
        resp.result.footer[0].implProjMoneyTotal = _toDecimal2(resp.result.footer[0].implProjMoneyTotal);
        resp.result.footer[0].implProjMoneyFundBu = _toDecimal2(resp.result.footer[0].implProjMoneyFundBu);
        resp.result.footer[0].implProjMoneyFundSheng = _toDecimal2(resp.result.footer[0].implProjMoneyFundSheng);
        resp.result.footer[0].implProjMoneyFundGao = _toDecimal2(resp.result.footer[0].implProjMoneyFundGao);
        resp.result.footer[0].implProjMoneyFundShi = _toDecimal2(resp.result.footer[0].implProjMoneyFundShi);
        resp.result.footer[0].implProjMoneyFundQi = _toDecimal2(resp.result.footer[0].implProjMoneyFundQi);
        resp.result.footer[0].implProjMoneyFundOther = _toDecimal2(resp.result.footer[0].implProjMoneyFundOther);
        resp.result.footer[0].investPlanTotal = _toDecimal2(resp.result.footer[0].investPlanTotal);
        resp.result.footer[0].investPlanFundBu = _toDecimal2(resp.result.footer[0].investPlanFundBu);
        resp.result.footer[0].investPlanFundSheng = _toDecimal2(resp.result.footer[0].investPlanFundSheng);
        resp.result.footer[0].investPlanFundGao = _toDecimal2(resp.result.footer[0].investPlanFundGao);
        resp.result.footer[0].investPlanFundShi = _toDecimal2(resp.result.footer[0].investPlanFundShi);
        resp.result.footer[0].investPlanFundQi = _toDecimal2(resp.result.footer[0].investPlanFundQi);
        resp.result.footer[0].investPlanFundOther = _toDecimal2(resp.result.footer[0].investPlanFundOther);
        resp.result.footer[0].fundPlanMoneyTotal = _toDecimal2(resp.result.footer[0].fundPlanMoneyTotal);
        resp.result.footer[0].fundPlanMoneyFundBu = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundBu);
        resp.result.footer[0].fundPlanMoneyFundSheng = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundSheng);
        resp.result.footer[0].fundPlanMoneyFundGao = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundGao);
        resp.result.footer[0].fundPlanMoneyFundShi = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundShi);
        resp.result.footer[0].fundPlanMoneyFundQi = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundQi);
        resp.result.footer[0].fundPlanMoneyFundOther = _toDecimal2(resp.result.footer[0].fundPlanMoneyFundOther);

        $("#vMoneyTable").empty();
        $("#vMoneyTable").datagrid({
            nowrap:false,
            fitColumns:true,
            pagination:true,
            showFooter: true,
            columns:com.im.fixedMoneyManage.baos.moneyColumns,
            data:resp.result
        });
        var p = $('#vMoneyTable').datagrid('getPager');
        $(p).pagination({
            pageSize: 20,
            pageList: [10,20,30],
            beforePageText: '第',
            afterPageText: '页    共 {pages}  页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
            onSelectPage:function(pageNumber, pageSize){
                _queryMoneys(pageNumber);
            }
        });
    }

    function _showEditMoney(index) {
        if(index == undefined) {
            $('#addEditModal .modal-title').html('固定资产新增');
            $("#fixAssetId").val('');
            $("#planTypeSel2").val($("#planTypeSel").val()).trigger("change");
            $("#orgUnit2").val($("#orgUnit").val()).trigger("change");
            if($("#impleProjName").val() == "") {
                $("#impleProjName2").find("option:selected").attr("selected", false);
                $("#impleProjName2").val($($("#impleProjName2").children()[1]).attr("value"));
            } else {
                $("#impleProjName2").val($("#impleProjName").val()).trigger("change");
            }
            $("#yearDt").val($("#year").val());
            //$("#endDt").val('');
            //$("#states").val('10').trigger("change");

            $("#buFund1").val("");
            $("#shengFund1").val("");
            $("#gaoFund1").val("");
            $("#qiFund1").val("");
            $("#shiFund1").val("");
            $("#otherFund1").val("");
            $("#total1").val("");

            $("#buFund2").val("");
            $("#shengFund2").val("");
            $("#gaoFund2").val("");
            $("#qiFund2").val("");
            $("#shiFund2").val("");
            $("#otherFund2").val("");
            $("#total2").val("");

            $("#buFund3").val("");
            $("#shengFund3").val("");
            $("#gaoFund3").val("");
            $("#qiFund3").val("");
            $("#shiFund3").val("");
            $("#otherFund3").val("");
            $("#total3").val("");

            $("#buFund4").val("");
            $("#shengFund4").val("");
            $("#gaoFund4").val("");
            $("#qiFund4").val("");
            $("#shiFund4").val("");
            $("#otherFund4").val("");
            $("#total4").val("");

            $("#description").val("");

        } else {
            var row = $('#vMoneyTable').datagrid('getData').rows[index];
            if(row.statusCd != 10) {
                toastr.success("当前状态不允许修改");
                return;
            }
            $('#addEditModal .modal-title').html('固定资产修改');
            $("#fixAssetId").val(row.fixAssetId);
            $("#orgUnit2").val(row.orgId).trigger("change");
            $("#impleProjName2").val(row.impleProjId).trigger("change");
            $("#yearDt").val(row.yearDt);
            //$("#endDt").val(row.endDt);
            //$("#states").val(row.statusCd).trigger("change");
            $("#description").val(com.im.common.util.descapeTextArea(row.description));

            var fund1 = _getFundDto(row.fundDetailVo,'planProjMoney');
            var fund2 = _getFundDto(row.fundDetailVo,'implProjMoney');
            var fund3 = _getFundDto(row.fundDetailVo,'investPlanMoney');
            var fund4 = _getFundDto(row.fundDetailVo,'fundPlanMoney');

            $("#buFund1").val(fund1.fundBu);
            $("#shengFund1").val(fund1.fundSheng);
            $("#gaoFund1").val(fund1.fundGao);
            $("#qiFund1").val(fund1.fundQi);
            $("#shiFund1").val(fund1.fundShi);
            $("#otherFund1").val(fund1.fundOther);
            $("#total1").val(fund1.total);

            $("#buFund2").val(fund2.fundBu);
            $("#shengFund2").val(fund2.fundSheng);
            $("#gaoFund2").val(fund2.fundGao);
            $("#qiFund2").val(fund2.fundQi);
            $("#shiFund2").val(fund2.fundShi);
            $("#otherFund2").val(fund2.fundOther);
            $("#total2").val(fund2.total);

            $("#buFund3").val(fund3.fundBu);
            $("#shengFund3").val(fund3.fundSheng);
            $("#gaoFund3").val(fund3.fundGao);
            $("#qiFund3").val(fund3.fundQi);
            $("#shiFund3").val(fund3.fundShi);
            $("#otherFund3").val(fund3.fundOther);
            $("#total3").val(fund3.total);

            $("#buFund4").val(fund4.fundBu);
            $("#shengFund4").val(fund4.fundSheng);
            $("#gaoFund4").val(fund4.fundGao);
            $("#qiFund4").val(fund4.fundQi);
            $("#shiFund4").val(fund4.fundShi);
            $("#otherFund4").val(fund4.fundOther);
            $("#total4").val(fund4.total);

        }
        $("#addEditModal").modal("show");
    }

    function _editMoney() {
        if(!_formValidate($("#addEditMoneyForm"))) {
            return;
        }

        var req = {};
        req.impleProjId= $("#impleProjName2 option:selected").attr("value");
        req.yearDt = $("#yearDt").val() + "-01-01";
        //req.endDt = $("#endDt").val();
        req.fundSourceBu = $("#buFund4").val();
        req.fundSourceSheng = $("#shengFund4").val();
        req.fundSourceGao = $("#gaoFund4").val();
        req.fundSourceQi = $("#qiFund4").val();
        req.fundSourceShi = $("#shiFund4").val();
        req.total = $("#total4").val();
        req.statusCd = "10";
        req.description = com.im.common.util.escapeTextArea($("#description").val());

        if($("#fixAssetId").val() == '') {
            var result = client.callAsJson("fixAssetService/addFixAsset", req);
            if(result.resultCode=='0'){
                toastr.success("固定资产增加成功");
                _queryMoneys(1);
                $("#addEditModal").modal("hide");
            }else {
                toastr.error("服务器忙！请稍后再试！"+result.resultMsg);
            }
        }else{
            req.fixAssetId = $("#fixAssetId").val();
            var result = client.callAsJson("fixAssetService/updateFixAsset", req);
            if(result.resultCode=='0'){
                toastr.success("固定资产修改成功");
                _queryMoneys(1);
                $("#addEditModal").modal("hide");
            }else {
                toastr.error("服务器忙！请稍后再试！"+result.resultMsg);
            }
        }
    }

    function _deleteMoney(index) {
        var row = $('#vMoneyTable').datagrid('getData').rows[index];
        if(row.statusCd != 10) {
            toastr.success("当前状态不允许删除");
            return;
        }

        com.im.common.confirm("确认是否删除固定资产", null, function () {
            var req = {};
            req.fixAssetId = row.fixAssetId;
            req.statusCd = 30;
            var resp = client.callAsJson("fixAssetService/updateFixAsset", req);
            if (!resp.success) {
                toastr.error(resp.resultMsg);
                return;
            }
            toastr.success("删除固定资产成功");
            _queryMoneys(1);
        })
    }

    function _dateFormat(date) {
        var date = new Date(date);
        return date.getFullYear();
    }

    //数据类型
    //sourceValue,源数据
    //colName,列名
    //type,资金计划类型
    function _getColVal(sourceValue,colName,type){

        var val;
        $.each(sourceValue.fundDetailVo,function (i,obj) {
            if(obj.type  == type){
                val = obj[colName];
                return false;
            }
        });

        if(val == undefined || val == ''){
            val = '0';
        }
        return val;

    };

    //sourceValue,源数据
    //colName,列名
    //type,资金计划类型
    function _getFundDto(sourceValue,type){

        var val;
        $.each(sourceValue,function (i,obj) {
            if(obj.type  == type){
                val = obj;
                return false;
            }
        });

        if(val == undefined || val == ''){
            val = {};
            val.type =  type;
            val.total = '0';
            val.fundBu = '0';
            val.fundSheng = '0';
            val.fundGao = '0';
            val.fundShi = '0';
            val.fundQi = '0';
            val.fundOther = '0';
            //val.statusCd;
            //val.remark;
            //val.fileName;
        }
        return val;

    };
    
    function _batchAudit() {
        var checkedItems = $('#vMoneyTable').datagrid('getChecked');
        if(checkedItems.length == 0) {
            toastr.error("请先选择条目");
            return;
        }
        var olist = [];
        $.each(checkedItems,function (i,obj) {
            //状态，未确认
            if(obj.statusCd == 10){
                olist.push(obj.fixAssetId);
            }
        });

        if(olist.length > 0){
            var param = {};
            param.fixAssetIds = olist;
            var result = client.callAsJson("fixAssetService/updateFixAssetStatus", param);
            if(result.resultCode=='0'){
                _queryMoneys(1);
            }else {
                toastr.error("服务器忙！请稍后再试！"+result.resultMsg);
                return;
            }
        }
        toastr.success("操作成功！");
    }
    
    function _export() {
        var vYear = $("#year").val();
        var req = {
            "classes": "FixAssetService",
            "fileName": "固定资产列表",
            "method": "queryFixAssetListForUI",
            "title": {
                "impleProjName":"项目名称",
                "orgName": "建设单位",
                "yearDt": "年份",
                "statusName": "状态",
                "fundDetailVo.planProjMoney.total": "合计$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundBu": "部补助$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundSheng": "省财政$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundGao": "政府融资$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundShi": "市州配套$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundQi": "企业自筹$概算总投资（单位：万元）",
                "fundDetailVo.planProjMoney.fundOther": "其他$概算总投资（单位：万元）",
                "fundDetailVo.implProjMoney.total": "合计$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundBu": "部补助$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundSheng": "省财政$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundGao": "政府融资$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundShi": "市州配套$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundQi": "企业自筹$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.implProjMoney.fundOther": "其他$累计至"+(vYear - 1)+"年下达的计划（单位：万元）",
                "fundDetailVo.investPlanMoney.total": "合计$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundBu": "部补助$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundSheng": "省财政$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundGao": "政府融资$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundShi": "市州配套$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundQi": "企业自筹$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.investPlanMoney.fundOther": "其他$累计至"+(vYear - 1)+"年完成的投资（单位：万元）",
                "fundDetailVo.fundPlanMoney.total": "合计$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundBu": "部补助$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundSheng": "省财政$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundGao": "政府融资$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundShi": "市州配套$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundQi": "企业自筹$本次完成投资",
                "fundDetailVo.fundPlanMoney.fundOther": "其他$本次完成投资"
            }
        };
        req.param={};
        req.param.year = vYear;
        req.param.planType = $("#planTypeSel option:selected").attr("value");
        req.param.developOrg = $("#orgUnit option:selected").attr("value");
        req.param.impleProjId = $("#impleProjName option:selected").attr("value");
        var url='/InfoManageWeb/commonService/export?json='+encodeURI(JSON.stringify(req));
        window.open(url, '_blank');
    }

    function _toDecimal2(x) {
        var f = parseFloat(x);
        if (isNaN(f)) {
            return false;
        }
        var f = Math.round(x*100)/100;
        var s = f.toString();
        var rs = s.indexOf('.');
        if (rs < 0) {
            rs = s.length;
            s += '.';
        }
        while (s.length <= rs + 2) {
            s += '0';
        }
        return s;
    }

    return {
        init : _init,
        showEditMoney : _showEditMoney,
        deleteMoney : _deleteMoney
    }
}();